// pages/group/index.ts
import { formatMessageDate } from '../../../utils/format';
import tran from '../../../utils/language'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _t: tran._t('hotDetail'),
    _tBase: tran._t('base'),
    navBarHeight: getApp().globalData.navBarHeight,
    showEventPopup: true,
    info: {},
    messageData: [] as messageType[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    if (options.info) {
      this.setData({
        info: JSON.parse(options.info)
      })
    }
    this.formatDate([{
      id: '43255',
      messageType: 'text',
      message: 'hello',
      sender: false,
      avatar: 'https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg',
      timestamp: 1732699424,
      isRead: false,
    },{
      id: '432525',
      messageType: 'text',
      message: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris.',
      sender: true,
      avatar: 'https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg',
      timestamp: 1732699685,
      isRead: false,
    }, {
      id: '432524',
      messageType: 'text',
      message: 'hhhh',
      sender: true,
      avatar: 'https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg',
      timestamp: 1732775160,
      isRead: false,
    }, {
      id: '432526',
      messageType: 'image',
      fileUrl: 'https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg',
      message: 'Nemi enim ipsam voluptatem quia voluptas sit',
      sender: false,
      avatar: 'https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg',
      timestamp: 1732860965,
      isRead: false
    }])
  },
  closePopup() {
    this.setData({ showEventPopup: false })
  },
  onClosePopup(e: baseEvent) {
    const leave = e.target.dataset.leave
    leave && console.log('离开群组');
    const customPopup = this.selectComponent('#event-popup');
    customPopup && customPopup.hidePopup()
  },
  onBack() {
    this.setData({ showEventPopup: true })
  },
  formatDate(data: messageType[]) {
    const newArr: messageType[] = []
    let lastdate = ''
    data.forEach((i: messageType) => {
      const {date,createTime,createDate} = formatMessageDate(i.timestamp)
      newArr.push({
        ...i,
        createTime,
        createDate: lastdate !== createDate? date :'',
      })
      lastdate = createDate
    });
    this.setData({ messageData: newArr })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})